package com.vs.service.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.vs.dao.VehicleDao;
import com.vs.pojo.Maintenance;
import com.vs.pojo.Vehicle;
import com.vs.service.VehicleService;

public class VehicleServiceImpl implements VehicleService {
	private VehicleDao vehicleDao;
	public void setVehicleDao(VehicleDao vehicleDao) {
		this.vehicleDao = vehicleDao;
	}
	
	
	@Override
	public void add() {
		Vehicle vehicle = new Vehicle();
		vehicle.setName("滚筒车");
		vehicle.setPlateNum("123456");
		
		Maintenance maintenance = new Maintenance();
		maintenance.setContext("常规维护");
		maintenance.setDate(new Date());
		maintenance.setVehicle(vehicle);
		Set<Maintenance> maintenances = new HashSet<Maintenance>();
		maintenances.add(maintenance);
		vehicle.setVehicleMaintenance(maintenances);
		
		vehicleDao.save(vehicle);
	}
	
	@Override
	public void remove() {
		List<Vehicle> vehicles = vehicleDao.getAll();
		for(Vehicle v:vehicles){
			vehicleDao.remove(v);
		}
	}
	
	
	@Override
	public void update() {
		List<Vehicle> vehicles = vehicleDao.getAll();
		for(Vehicle v:vehicles){
			Maintenance maintenance = new Maintenance();
			maintenance.setContext("更新维护...");
			maintenance.setDate(new Date());
			maintenance.setVehicle(v);
			Set<Maintenance> maintenances = v.getVehicleMaintenance();
			maintenances.add(maintenance);
			v.setVehicleMaintenance(maintenances);
			vehicleDao.saveOrUpdate(v);
		}
	}
	
	@Override
	public void removeRelation() {
		List<Vehicle> vehicles = vehicleDao.getAll();
		for(Vehicle v:vehicles){
			Set<Maintenance> x= v.getVehicleMaintenance();
			x.removeAll(x);
			v.setVehicleMaintenance(x);
			vehicleDao.update(v);
		}
	}
}
